/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "tile.h"


namespace tri
{



tri::TVector loadVector(tri::TXMLNode* node){
	tri::TVector v;
	v.x = node->getAttribute<float>("x");
	v.y = node->getAttribute<float>("y");
	v.z = node->getAttribute<float>("z");
	return v;
}

TilePlane::TilePlane(int type) : 
	tri::TEntity(),
	m_PlaneType(type),
	mObject(NULL){
}

TilePlane::~TilePlane(){
	deleteModel();
}

void TilePlane::display(){
	
	tri::applyTexture(getImage());
	TVector size = getSize();
	
	float heightMap[4] = {0, 0, 0, 0};
	
	float rotY = 0;
	
	glDisable(GL_CULL_FACE);
	glPushMatrix();
	graphics::rotate(tri::TVector(-90, 0, rotY));
	glScalef(size.x, size.y, size.z);
	glBegin(GL_TRIANGLE_STRIP);	
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f,  -0.5f, heightMap[0]);
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f,  -0.5f, heightMap[1]);
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f,  0.5f, heightMap[2]);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f,  0.5f, heightMap[3]);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f,  -0.5f, heightMap[0]);
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f,  -0.5f, heightMap[1]);
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f,  0.5f, heightMap[2]);
	glEnd();	
	glPopMatrix();
	glEnable(GL_CULL_FACE);
}

void TilePlane::deleteModel(){
	if (mObject != NULL){
		delete mObject;
		mObject = NULL;
	}
}

void TilePlane::setModel(tri::TEntity* Object){
	deleteModel();
	mObject = Object;
}


/**
 * 
 * TILE CHACHE
 * 
 */

TileTemplateCache::TileTemplateCache(){
	
}

TileTemplateCache::~TileTemplateCache(){
	clear();
}

TilePlane* TileTemplateCache::getTile(unsigned int id){
	TileTemplate* rTemplate = mTileTemplateList[id];
	if (rTemplate!=NULL){
		return rTemplate->getTile();
	}
	
	return NULL;
}

void TileTemplateCache::clear(){
	std::map<unsigned int, TileTemplate*>::iterator it;
	for ( it=mTileTemplateList.begin() ; it != mTileTemplateList.end(); it++ ){
		delete (*it).second;
	}
	mTileTemplateList.clear();
}

void TileTemplateCache::getTileTemplateList(std::vector<TileTemplate*> &list){
	list.clear();
	std::map<unsigned int, TileTemplate*>::iterator it;
	for ( it=mTileTemplateList.begin() ; it != mTileTemplateList.end(); it++ ){
		list.push_back( (*it).second );
	}
}

void TileTemplateCache::addTemplate(tri::TXMLNode* node){
	TileTemplate* rTemplate = new TileTemplate();
	rTemplate->load(node);
	mTileTemplateList[rTemplate->getID()] = rTemplate;
}

/**
 * 
 * TILE TEMPLATE
 * 
 */

TileTemplate::TileTemplate():
	dimensionX(1),dimensionY(1),
	mTileType(TILE_PLANE)
{}

TileTemplate::~TileTemplate(){
}

void TileTemplate::load(tri::TXMLNode* tile_node){
	
	//tile type
	if(tile_node->hasAttribute("type")){
		mTileType = getTileTypeFromString(tile_node->getAttribute<std::string>("type"));
	}
	//tile id
	mID = (unsigned int)tile_node->getAttribute<int>("id");
	 
	//image
	if(tile_node->hasAttribute("image")){
		mImage = tile_node->getAttribute<std::string>("image");
	}
	
	if(tile_node->hasAttribute("icon")){
		mIcon = tile_node->getAttribute<std::string>("icon");
	}
	
	if(tile_node->hasAttribute("dimX")){
		dimensionX = tile_node->getAttribute<int>("dimX");
	}
	
	if(tile_node->hasAttribute("dimY")){
		dimensionY = tile_node->getAttribute<int>("dimY");
	}
}

TilePlane* TileTemplate::getTile(){
	TilePlane* plane = new TilePlane(mTileType);
	plane->setSize(dimensionX,dimensionY,1);
	plane->setImage(mImage);
	return plane;
}

unsigned int TileTemplate::getID(){
	return mID;
}


int TileTemplate::getTileTypeFromString(std::string t){
	if(t=="PLANE"){
		return TILE_PLANE;
	}
	else if(t=="INCLINED_NORTH"){
		return TILE_INCLINED_NORTH;
	}
	else if(t=="INCLINED_NORTH_EAST"){
		return TILE_INCLINED_NORTH_EAST;
	}
	else if(t=="INCLINED_EAST"){
		return TILE_INCLINED_EAST;
	}
	else if(t=="INCLINED_SOUTH_EAST"){
		return TILE_INCLINED_SOUTH_EAST;
	}
	else if(t=="INCLINED_SOUTH"){
		return TILE_INCLINED_SOUTH;
	}
	else if(t=="INCLINED_SOUTH_WEST"){
		return TILE_INCLINED_SOUTH_WEST;
	}
	else if(t=="INCLINED_WEST"){
		return TILE_INCLINED_WEST;
	}
	else if(t=="INCLINED_NORTH_WEST"){
		return TILE_INCLINED_NORTH_WEST;
	}
	return -1;
	
}
}
